<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 公开不授权控制器
//-------------------------

namespace app\domin\controller;

use app\domin\Base;
use think\Config;
use think\Db;
use think\Loader;
use think\exception\HttpException;


use com\gdl\newagk\interfaces\ResourcesServiceClient;
use com\gdl\newagk\dataobject\resources\ResourcesDTO; 



class Resources extends Base
{
	

	public function attendanceGroup()
    {

		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId/d') : 0;

		$userId = $this->request->param('userId') ? $this->request->param('userId/d') : 0;
		if (!$adminUserId  || !$userId) {
				$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
				return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new ResourcesServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new ResourcesDTO();
				$engprojectDo->adminUserId	= $adminUserId;
				$engprojectDo->userId		= $userId;
				$result = $api->attendanceGroup($engprojectDo);

				
		
				if($result->code){
					if(empty($result->data)){
						$data =  ['msg'=>$result->message,'code'=>'200','data'=>[]];
					} else {
						$attendanceg = $result->data;
						$adminuserattendance = $result->datas[0];
						$list = [];
						foreach($attendanceg as $v){
							$list[] = [
									'id'	=>$v['id'],
									'title'	=>$v['title'],
									'checked'	=>in_array($v['id'],$adminuserattendance) ? true : false,
									];
						}
	
						$data =  ['msg'=>'获取成功','code'=>'200','data'=>$list];
					}
				} else{
					$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'获取失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
		
	}
	
	//保存考勤组资源
	public function attendanceEdit()
    {

		$adminUserId = $this->request->post('adminUserId') ? $this->request->post('adminUserId/d') : 0;
		$userId = $this->request->post('userId') ? $this->request->post('userId/d') : 0;
		$resourc =  $this->request->post('resourc') ? explode(',', $this->request->post('resourc')) : [];
		///////////////////
		/*
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId/d') : 0;
		$userId = $this->request->param('userId') ? $this->request->param('userId/d') : 0;
		$resourc =  $this->request->param('resourc') ? explode(',', $this->request->param('resourc')) : [];
		*/
		///////////////////
		
		if (!$adminUserId  || !$userId) {
				$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
				return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new ResourcesServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new ResourcesDTO();
				$engprojectDo->adminUserId	= $adminUserId;
				$engprojectDo->userId	= $userId;
				$engprojectDo->filename	= $resourc;
				$result = $api->attendanceEdit($engprojectDo);

				if($result->code){
					$data =  ['msg'=>'操作成功','code'=>'200','data'=>[]];
					
				} else{
					$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'获取失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
		
	}
	
	
}
